package m3_publishsubscribe;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception{
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.126.129");
        //factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");

        Channel c = factory.newConnection().createChannel();

        //定义交换机
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //向交换机发送数据
        while (true){
            System.out.println("输入消息：");
            String msg = new Scanner(System.in).nextLine();

            //第二个参数，对于fanout交换机无效
            c.basicPublish("logs","",null,msg.getBytes());
        }
    }


}
